package com.nlp.visualization.controller.jersey.controller;

import com.google.gson.Gson;
import com.nlp.visualization.common.CONSTANTS;
import com.nlp.visualization.pojo.common.AjaxReturn;
import com.nlp.visualization.pojo.db.TUserSuggestions;
import com.nlp.visualization.service.backEnd.IUserSuggetionsService;
import com.nlp.visualization.utils.ConvertUtils;
import com.nlp.visualization.utils.TimeHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description:
 * @date 2018/5/10下午8:27
 */

@Controller
@Path("/suggestion")
public class SuggestionController {

    private Logger logger = LoggerFactory.getLogger(SuggestionController.class);
    private Gson gson = new Gson();

    @Autowired
    private IUserSuggetionsService suggetionsService;

    @POST
    @Path("/segment")
    @Consumes(MediaType.APPLICATION_JSON)
    public String saveSuggentions(String json) {

        Map map = ConvertUtils.jsonToMap(json);
        String words = (String) map.get("words");
        String type = (String) map.get("type") + "【未处理】";
        Long time = TimeHelper.getTime().longValue();
        //示例化一个建议
        TUserSuggestions suggestions = new TUserSuggestions();
        suggestions.setTime(time);
        suggestions.setType(type);
        suggestions.setWord(words);
        try {
            suggetionsService.save(suggestions);
        } catch (Exception e) {


            logger.error("保存用户建议异常", e);
            return gson.toJson(new AjaxReturn(CONSTANTS.ERROR_PARAM_ERROR, "成功"));
        }
        return gson.toJson(new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功"));

    }
}
